package com.ccys.doctor.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.common.vo.ResponseResultApp;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.doctor.flag.CreateFlagPatientDto;
import com.ccys.db.entity.TblDoctor;
import com.ccys.db.entity.TblFlagMember;
import com.ccys.db.entity.TblFlagReason;
import com.ccys.db.mapper.TblFlagMemberMapper;
import com.ccys.db.mapper.TblFlagReasonMapper;
import com.ccys.db.page.AppPageDto;
import com.ccys.db.vo.doctor.flag.FlagPatientMakeVo;
import com.ccys.db.vo.doctor.flag.GetFlagPatientVo;
import com.ccys.doctor.service.FlagService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: WhiteCoat
 * @description: 病人历史相关接口
 * @author: ZhangJingCan
 * @create: 2024-02-05 13:34
 **/

@Service
public class FlagServiceImpl implements FlagService {

    @Resource
    private TblFlagMemberMapper tblFlagMemberMapper;

    @Resource
    private TblFlagReasonMapper tblFlagReasonMapper;

    @Override
    public ResponseResultApp<GetFlagPatientVo> getFlagPatient(String type, String memberId, String keyword, Integer pageIndex, Integer pageSize) {
        if (ObjectUtil.isEmpty(type)) {
            return ResponseResultApp.error("The type field is required.");
        }
        if (ObjectUtil.isEmpty(memberId)) {
            return ResponseResultApp.error("The member_id field is required.");
        }
        if (ObjectUtil.isEmpty(pageIndex)) {
            return ResponseResultApp.error("The page_index field is required.");
        }
        if (ObjectUtil.isEmpty(pageSize)) {
            return ResponseResultApp.error("The page_size field is required.");
        }
        //开启分页
        AppPageDto.startPage();
        List<FlagPatientMakeVo> list = tblFlagMemberMapper.getPageFlagPatient(type, pageIndex, pageSize, memberId, keyword);
        PageInfo<FlagPatientMakeVo> pageInfo = new PageInfo<>(list);
        //准备返回的对象
        GetFlagPatientVo vo = new GetFlagPatientVo();
        vo.setTotalPagel(pageInfo.getPages());
        vo.setFlagPatient(list);
        return ResponseResultApp.success(vo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResultApp<GetFlagPatientVo> createFlagPatient(CreateFlagPatientDto dto) {
        //获取医生
        TblDoctor doctor = UserUtil.getCurLoginDoctor();

        //校验字段
        if (ObjectUtil.isEmpty(dto.getType())) {
            return ResponseResultApp.error("The type field is required.");
        }
        if (ObjectUtil.isEmpty(dto.getMember_id())) {
            return ResponseResultApp.error("The member_id field is required.");
        }

        //这个字段可以为空
        dto.setReason(dto.getReason() != null ? dto.getReason() : "");
        //根据type找Reason数据
        Example example = new Example(TblFlagReason.class);
        example.createCriteria()
                .andEqualTo("id", dto.getType())
                .andEqualTo("status", 1)
                .andEqualTo("delFlag", 0);
        TblFlagReason reason = tblFlagReasonMapper.selectOneByExample(example);
        if (ObjectUtil.isEmpty(reason)) {
            return ResponseResultApp.error("This label was not found. Please select a new label.");
        }
        //准备新增数据
        TblFlagMember tblFlagMember = new TblFlagMember();
        tblFlagMember.setFlagReasonId(dto.getType());
        tblFlagMember.setReason(dto.getReason());
        tblFlagMember.setMemberId(dto.getMember_id());
        tblFlagMember.setDoctorId(doctor.getId());
        tblFlagMemberMapper.insertSelective(tblFlagMember);

        //准备返回的数据
        //开启分页
        AppPageDto.startPage();
        List<FlagPatientMakeVo> list = tblFlagMemberMapper.getPageFlagPatient(null, 0, 1000, dto.getMember_id(), null);
        PageInfo<FlagPatientMakeVo> pageInfo = new PageInfo<>(list);
        //准备返回的对象
        GetFlagPatientVo vo = new GetFlagPatientVo();
        vo.setTotalPagel(pageInfo.getPages());
        vo.setFlagPatient(list);
        return ResponseResultApp.success(vo);
    }
}
